package com.jsptags.navigation.pager;

import com.jsptags.navigation.pager.parser.PageTagExport;
import com.jsptags.navigation.pager.parser.ParseException;
import com.jsptags.navigation.pager.parser.TagExportParser;
import javax.servlet.jsp.*;

@SuppressWarnings("serial")
public abstract class PageTagSupport extends PagerTagSupport {

	private String export;
	private PageTagExport pageTagExport;
	private Object oldPageUrl;
	private Object oldPageNumber;
	private Object oldFirstItem;
	private Object oldLastItem;

	public final void setExport(String s) throws JspException {
		if (export != s)
			try {
				pageTagExport = TagExportParser.parsePageTagExport(s);
			} catch (ParseException parseexception) {
				throw new JspTagException(parseexception.getMessage());
			}
		export = s;
	}

	public final String getExport() {
		return export;
	}

	protected final void setPageAttributes(int i) {
		if (pageTagExport == null) {
			pageContext.setAttribute("pageUrl", pagerTag.getPageUrl(i));
			pageContext.setAttribute("pageNumber", pagerTag.getPageNumber(i));
		} else {
			String s;
			if ((s = pageTagExport.getPageUrl()) != null)
				pageContext.setAttribute(s, pagerTag.getPageUrl(i));
			if ((s = pageTagExport.getPageNumber()) != null)
				pageContext.setAttribute(s, pagerTag.getPageNumber(i));
			int j = pagerTag.getMaxPageItems();
			if ((s = pageTagExport.getFirstItem()) != null) {
				int k = i * j + 1;
				pageContext.setAttribute(s, new Integer(k));
			}
			if ((s = pageTagExport.getLastItem()) != null) {
				int l = Math.min((i + 1) * j, pagerTag.getItemCount());
				pageContext.setAttribute(s, new Integer(l));
			}
		}
	}

	protected final void setOffsetAttributes(int i) {
		if (pageTagExport == null) {
			pageContext.setAttribute("pageUrl", pagerTag.getOffsetUrl(i));
			pageContext.setAttribute("pageNumber", pagerTag
					.getOffsetPageNumber(i));
		} else {
			String s;
			if ((s = pageTagExport.getPageUrl()) != null)
				pageContext.setAttribute(s, pagerTag.getOffsetUrl(i));
			if ((s = pageTagExport.getPageNumber()) != null)
				pageContext.setAttribute(s, pagerTag.getOffsetPageNumber(i));
			int j = pagerTag.getMaxPageItems();
			if ((s = pageTagExport.getFirstItem()) != null) {
				int k = i + 1;
				pageContext.setAttribute(s, new Integer(k));
			}
			if ((s = pageTagExport.getLastItem()) != null) {
				int l = Math.min(i + j, pagerTag.getItemCount());
				pageContext.setAttribute(s, new Integer(l));
			}
		}
	}

	protected final void removeAttributes() {
		if (pageTagExport == null) {
			pageContext.removeAttribute("pageUrl");
			pageContext.removeAttribute("pageNumber");
		} else {
			String s;
			if ((s = pageTagExport.getPageUrl()) != null)
				pageContext.removeAttribute(s);
			if ((s = pageTagExport.getPageNumber()) != null)
				pageContext.removeAttribute(s);
			if ((s = pageTagExport.getFirstItem()) != null)
				pageContext.removeAttribute(s);
			if ((s = pageTagExport.getLastItem()) != null)
				pageContext.removeAttribute(s);
		}
	}

	public int doStartTag() throws JspException {
		super.doStartTag();
		if (pageTagExport == null) {
			oldPageUrl = pageContext.getAttribute("pageUrl");
			oldPageNumber = pageContext.getAttribute("pageNumber");
		} else {
			String s;
			if ((s = pageTagExport.getPageUrl()) != null)
				oldPageUrl = pageContext.getAttribute(s);
			if ((s = pageTagExport.getPageNumber()) != null)
				oldPageNumber = pageContext.getAttribute(s);
			if ((s = pageTagExport.getFirstItem()) != null)
				oldFirstItem = pageContext.getAttribute(s);
			if ((s = pageTagExport.getLastItem()) != null)
				oldLastItem = pageContext.getAttribute(s);
		}
		return 1;
	}

	public int doEndTag() throws JspException {
		if (pageTagExport == null) {
			restoreAttribute("pageUrl", oldPageUrl);
			restoreAttribute("pageNumber", oldPageNumber);
			oldPageUrl = null;
			oldPageNumber = null;
		} else {
			String s;
			if ((s = pageTagExport.getPageUrl()) != null) {
				restoreAttribute(s, oldPageUrl);
				oldPageUrl = null;
			}
			if ((s = pageTagExport.getPageNumber()) != null) {
				restoreAttribute(s, oldPageNumber);
				oldPageNumber = null;
			}
			if ((s = pageTagExport.getFirstItem()) != null) {
				restoreAttribute(s, oldFirstItem);
				oldFirstItem = null;
			}
			if ((s = pageTagExport.getLastItem()) != null) {
				restoreAttribute(s, oldLastItem);
				oldLastItem = null;
			}
		}
		super.doEndTag();
		return 6;
	}

	public void release() {
		export = null;
		pageTagExport = null;
		oldPageUrl = null;
		oldPageNumber = null;
		oldFirstItem = null;
		oldLastItem = null;
		super.release();
	}

	public PageTagSupport() {
		export = null;
		pageTagExport = null;
		oldPageUrl = null;
		oldPageNumber = null;
		oldFirstItem = null;
		oldLastItem = null;
	}
}
